const BASE_URL = 'http://api-breakingnews-web.itheima.net'
axios
.get(`${BASE_URL }/my/article/cates`,{
  headers:{
    Authorization:localStorage.getItem("token")
  },
})
.then(res=>{
  console.log("获取类别",res.data.data)
  res.data.data.forEach(v=> {
    let theOption=`<option value="${v.Id}" lay-verify="cate">${v.name}</option>`
    $('select').append(theOption)
  })
  let form=window.layui.form
  form.render("select","edit")
})
initEditor()
let cropper
function initCropper() {
  const image=document.getElementById("image")
  cropper=new Cropper(image,{
    aspectRatio:1,
    preview:$(".img-preview"),
  })
}
initCropper()
$(".select").on("click",function(){
  $("#file").click()
})
$("#file").on("change",function(){
  console.log(this.files[0])
  let url=URL.createObjectURL(this.files[0])
  cropper.replace(url)
})
$(".layui-form").on("submit",function(e){
  e.preventDefault()
  let fd=new FormData(document.querySelector("form"))
  let htmlStr=tinyMCE.activeEditor.getContent()
  fd.set("content",htmlStr)
  let canvas=cropper.getCroppedCanvas({
    width:100,
    height:100,
  })
  canvas.toBlob(function(blobObj){
    fd.append("cover_img",blobObj)
    axios
    .post(`${BASE_URL}/my/article/add`,fd,{
      headers:{
        Authorization: localStorage.getItem('token'),
      },
    })
    .then(res=>{
      console.log("发表文章结果：",res.data)
      if(res.data.status===0){
        layer.msg(res.data.message,{icon:1},function(){
          location.href="../list/list.html"
        })
      }
    })
  })
})